﻿using WRJClient.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WRJClient.UI.Map;
using Esri.ArcGISRuntime.Geometry;
using WRJClient.Utils;

namespace WRJClient.ViewModel
{
    public class ZoneViewModel : BaseViewModel<ZoneModel>, IMapObject
    {
        private BaseGraphic _gArea = new BaseGraphic();
        private BaseGraphic _gIcon = new BaseGraphic();

        public BaseGraphic GArea
        {
            get { return _gArea; }
        }
        public bool Visable { get { return _gArea.IsVisible || _gIcon.IsVisible; }
            set
            {
                _gArea.IsVisible = value;
                _gIcon.IsVisible = value;
            }
        }

        public bool IsVisable
        {
            get { return _gArea.IsVisible; }
            set { if (_gArea.IsVisible != value) { _gArea.IsVisible = value; _gIcon.IsVisible = value; OnPropertyChanged(); }; }
        }
        public bool IsEnabled { get { return ArcgisUtils.IsExtentValid(_gArea.Geometry.Extent); } }

        public string Type
        {
            get
            {
                string type = "";
                if (StaticData != null)
                {
                    switch (StaticData.Type)
                    {
                        case 0:
                            type = "禁飞区";
                            break;
                        case 1:
                            type = "警戒区";
                            break;
                        case 2:
                            type = "干扰区";
                            break;
                        case 3:
                            type = "雷暴区";
                            break;
                        case 4:
                            type = "其他";
                            break;
                    }
                }
                return type;
            }
        }

        public ZoneViewModel(ZoneModel model = null) : base(model)
        {
            InitGraphics();
        }

        public void InitGraphics()
        {
            GraphicFactory.CreatePolygonGraphic(_gArea, StaticData.DbId, GraphicPart.Base, SymbolHelper.Instance.GetFillSymbolInfo(GetType(), "禁飞区"), StaticData.Data);

            MapPoint mappoint = _gArea.Geometry.Extent.GetCenter();
            MapPoint center = (MapPoint)GeometryEngine.Project(mappoint, SpatialReferences.Wgs84);
            GraphicFactory.CreateImageGraphic(_gIcon, StaticData.DbId, GraphicPart.ZoneIcon, SymbolHelper.Instance.GetPictureMarkerSymbolInfo(GetType(), "禁飞图标"), center.X, center.Y);

        }

        public void AddGraphics()
        {
            MapManager.Instance.AddGraphic("Layer2", _gArea);
            MapManager.Instance.AddGraphic("Layer3", _gIcon);
        }
        public void RemoveGraphics()
        {
            MapManager.Instance.RemoveGraphic("Layer2", _gArea);
            MapManager.Instance.RemoveGraphic("Layer3", _gIcon);
        }
    }
}
